//
//  UFOYZXVideoViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 1/11/16.
//  Copyright © 2016 dllo. All rights reserved.
//

#import "UFOYZXVideoViewController.h"
#import "UFOYZXVideoCollectionView.h"
#import "UIImageView+WebCache.h"
#import "UFOYZXVideoModel.h"
#import "UFOBaseModel.h"
#import "LQCAFNetTool.h"
#import "UFOYZXModel.h"
#import "UFOYZXTypeViewController.h"
@interface UFOYZXVideoViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowL;
@property (nonatomic, retain) NSMutableArray *videoArr;
@property (nonatomic, retain) UISegmentedControl *segmentC;
@property (nonatomic, retain) NSMutableArray *subCategorysArr;
@end

@implementation UFOYZXVideoViewController
- (void)dealloc
{
    [_subCategorysArr release];
    self.collectionV.delegate = nil;
    self.collectionV.dataSource = nil;
    [_collectionV release];
    [_flowL release];
    [_segmentC release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createCollectionView];
    
    self.segmentC = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"推荐",@"解说",@"搞笑",@"综合",@"英雄", nil]];
    self.segmentC.frame = CGRectMake(0, 65 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, 30 * SCREENHEIGHT / HEIGHTSIXP);
    self.segmentC.backgroundColor = [UIColor colorWithWhite:0.745 alpha:1.000];
    
    self.segmentC.selectedSegmentIndex = 0;
    self.segmentC.tintColor = [ UIColor whiteColor];
    
    [self.segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.segmentC];

    [self.segmentC release];
    
    [self getData];
    
    
}

- (void)segmentAction:(UISegmentedControl *)sender
{
    if (self.segmentC.selectedSegmentIndex == 0) {
        if (0 != self.videoArr.count) {
                
            UFOYZXModel *model = self.videoArr[sender.selectedSegmentIndex];
            self.subCategorysArr = model.subCategorys;
                
            [self.collectionV reloadData];
        }
    }
    if (self.segmentC.selectedSegmentIndex == 1) {
        if (0 != self.videoArr.count) {
                
            UFOYZXModel *model = self.videoArr[sender.selectedSegmentIndex];
            self.subCategorysArr = model.subCategorys;
                
            [self.collectionV reloadData];
        }
    }
    if (self.segmentC.selectedSegmentIndex == 2) {
           if (0 != self.videoArr.count) {
            UFOYZXModel *model = self.videoArr[sender.selectedSegmentIndex];
            self.subCategorysArr = model.subCategorys;
            
            [self.collectionV reloadData];
        }
    }
    if (self.segmentC.selectedSegmentIndex == 3) {
        if (0 != self.videoArr.count) {
            UFOYZXModel *model = self.videoArr[sender.selectedSegmentIndex];
            self.subCategorysArr = model.subCategorys;
            
            [self.collectionV reloadData];
        }
    }
    if (self.segmentC.selectedSegmentIndex == 4) {
        if (0 != self.videoArr.count) {
            UFOYZXModel *model = self.videoArr[sender.selectedSegmentIndex];
            self.subCategorysArr = model.subCategorys;
            
            [self.collectionV reloadData];
        }
    }
}



#pragma 创建collectionView
- (void)createCollectionView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"视频";
    
    self.flowL = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    
    self.flowL.minimumLineSpacing = 50 * SCREENWIDTH / WIDTHSIXP;
    self.flowL.headerReferenceSize = CGSizeMake(10 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP);
    _flowL.itemSize = CGSizeMake(170 * SCREENWIDTH / WIDTHSIXP, 220 * SCREENHEIGHT / HEIGHTSIXP);
    self.collectionV.frame = CGRectMake(0, 0, SCREENWIDTH, 950 * SCREENHEIGHT / HEIGHTSIXP);
    self.flowL.sectionInset = UIEdgeInsetsMake(0, 10 * SCREENWIDTH / WIDTHSIXP, 0, 10 * SCREENWIDTH / WIDTHSIXP);
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, SCREENHEIGHT) collectionViewLayout:self.flowL];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.view addSubview:self.collectionV];
    [self.collectionV release];
    [self.collectionV registerClass:[UFOYZXVideoCollectionView class] forCellWithReuseIdentifier:@"cell"];
}

#pragma 获取数据
- (void)getData
{

    self.videoArr = [NSMutableArray array];
    NSString *url = @"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=c";
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        self.videoArr = [UFOYZXModel baseModelWithArr:arr];
        
        if (0 != self.videoArr.count) {
        
            
            UFOYZXModel *model = self.videoArr[self.segmentC.selectedSegmentIndex];
            self.subCategorysArr = model.subCategorys;
        }
       
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    UFOYZXTypeViewController *typeVC = [[UFOYZXTypeViewController alloc] init];
    UFOYZXVideoModel *model = self.subCategorysArr[indexPath.row];
    typeVC.tagL = model.tag;
    typeVC.nameL = model.name;
    [self.navigationController pushViewController:typeVC animated:YES];
    
}

// cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.subCategorysArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    UFOYZXVideoCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    
    if (0 != self.subCategorysArr.count) {
        UFOYZXVideoModel *model = self.subCategorysArr[indexPath.row];
        [cell.titleImage sd_setImageWithURL:[NSURL URLWithString:model.icon]];
        cell.signname.text = model.name;
    }

    
    
    
    
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
